//
//  SelectGroupListController.m
//  UESTCPioneer
//
//  Created by Apple on 14-11-17.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "SelectGroupListController.h"
#import "CoreDataEntity.h"
#import "CustomContactSectionController.h"
#import "EMTextView.h"
#import "EMRemarkImageView.h"

@interface SelectGroupListController ()<UITableViewDataSource,UITableViewDelegate,EMChooseViewDelegate>
{
    UITableView * _tableView;
    NSArray * _listSource;
}
@property (strong, nonatomic) NSArray * blockUsernames;
@property (strong, nonatomic) NSMutableArray *selectedContacts;
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) UIScrollView *footerScrollView;
@property (strong, nonatomic) UIButton *doneButton;

@property (nonatomic) BOOL isMemberOn;
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) EMTextView *textView;

@property (nonatomic) BOOL isPublic;
@property (strong, nonatomic) UILabel *groupTypeLabel;//群组类型
@end

@implementation SelectGroupListController

- (instancetype)initWithBlockSelectedUsernames:(NSArray *)blockUsernames{
    self = [super init];
    if (self) {
        _blockUsernames = [NSArray arrayWithArray:blockUsernames];
        [self reloadFooterView];
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"选择列表";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
    //初始化数据源
    if (_blockUsernames.count == 0) {
        _blockUsernames = [NSArray array];
    }
    _selectedContacts = [NSMutableArray array];
    _listSource = [CoreDataEntity selectAllSubParty];
    //添加UITableView
    [self addTableView];
}

-(void)addTableView{
    [self.view addSubview:self.footerView];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.footerView.frame.size.height - 60) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    UILabel * headLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 20)];
    headLabel.text = @"从党支部分组中选择";
    headLabel.textAlignment = NSTextAlignmentCenter;
    headLabel.textColor = [UIColor grayColor];
    headLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    _tableView.tableHeaderView = headLabel;
    [self.view addSubview:_tableView];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    SubPartyEntity * entity = _listSource[indexPath.row];
    cell.textLabel.text = entity.partyName;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SubPartyEntity * entity = _listSource[indexPath.row];
    CustomContactSectionController *selectionController = [[CustomContactSectionController alloc] initWithPartyId:entity.partyNo  blockUsernames:_blockUsernames successBlock:^(NSMutableArray *results) {
        [_selectedContacts addObjectsFromArray:results];
        [self reloadFooterView];
    } failureBloack:^(NSError *error) {
        NSLog(@"出错了!");
    }];
    selectionController.delegate = self;
    [self.navigationController pushViewController:selectionController animated:YES];
}

- (UIView *)footerView
{
    if (_footerView == nil) {

        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 47, self.view.frame.size.width, 50)];
        _footerView.backgroundColor = [UIColor whiteColor];
        
        _footerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
        _footerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 2, _footerView.frame.size.width - 30 - 70, _footerView.frame.size.height - 7)];
        [_footerView addSubview:_footerScrollView];
        
        _doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_footerView.frame.size.width - 80, 9, 70, _footerView.frame.size.height - 16)];
        [_doneButton setBackgroundColor:kNavigationBarColor];
        [_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _doneButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_doneButton setTitle:@"确定" forState:UIControlStateNormal];
        [_doneButton.layer setCornerRadius:5];
        [_doneButton addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:_doneButton];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _footerView.frame.size.width, 2)];
        label.backgroundColor = [UIColor grayColor];
        [_footerView addSubview:label];
    }
    
    return _footerView;
}

- (void)reloadFooterView
{
    [self.footerScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat imageSize = self.footerScrollView.frame.size.height;
    NSInteger count = [self.selectedContacts count];
    self.footerScrollView.contentSize = CGSizeMake(imageSize * count, imageSize);
    for (int i = 0; i < count; i++) {
        EMBuddy *buddy = [self.selectedContacts objectAtIndex:i];
        EMRemarkImageView *remarkView = [[EMRemarkImageView alloc] initWithFrame:CGRectMake(i * imageSize, 0, imageSize, imageSize)];
        remarkView.image = [UIImage imageNamed:@"chatListCellHead.png"];
        remarkView.remark = [CoreDataEntity selectUserNameWithUserId:buddy.username];
        [self.footerScrollView addSubview:remarkView];
    }
    
    if ([self.selectedContacts count] == 0) {
        [_doneButton setTitle:@"确定" forState:UIControlStateNormal];
    }
    else{
        [_doneButton setTitle:[NSString stringWithFormat:@"确定(%i)", [self.selectedContacts count]] forState:UIControlStateNormal];
    }
    
}

#pragma mark - EMChooseViewDelegate
- (void)viewController:(EMChooseViewController *)viewController didFinishSelectedSources:(NSArray *)selectedSources
{
    [_delegate viewController:nil didFinishSelectedSources:_selectedContacts];
}

-(void)doneAction:(id)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(viewController:didFinishSelectedSources:)]) {
         [_delegate viewController:nil didFinishSelectedSources:_selectedContacts];
    }
    [self.leveyTabBarController.navigationController popViewControllerAnimated:YES];
}
@end
